<template>
  <div class="myroom_box">
    <el-table :data="tableData" style="width: 100%" stripe>
      <el-table-column
        show-overflow-tooltip
        type="index"
        label="序号"
        :index="tableIndex"
      >
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="title" label="标题">
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="address" label="地址">
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="description" label="描述">
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="address" label="标签">
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="price" label="月租">
        <template slot-scope="scope">
          <span> {{ scope.row.price | priceFilter }} </span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="status" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">未审核</span>
          <span v-else-if="scope.row.status == 1">正常</span>
          <span v-else-if="scope.row.status == 2">违规</span>
          <span v-else-if="scope.row.status == 3">售出</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="modifyTime" label="上次修改">
        <template slot-scope="scope">
          <span> {{ scope.row.modifyTime | timeFilter }} </span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="createTime" label="创建时间">
        <template slot-scope="scope">
          <span> {{ scope.row.createTime | timeFilter }} </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div class="option">
            <el-button size="big" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              title="确认删除？"
              hide-icon
              confirm-button-type="Text"
              cancel-button-type="primary"
              @confirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button size="big" type="danger" slot="reference">
                删除
              </el-button>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      background
      hide-on-single-page
      class="pagination"
      :page-size="size"
      :current-page="current"
      :total="total"
      @prev-click="getTableData"
      @next-click="getTableData"
      @current-change="getTableData"
    >
    </el-pagination>
    <EditRoom ref="editDrawer" />
  </div>
</template>

<script>
import axios from "@/request";
import { Loading } from "element-ui";

import EditRoom from "@/components/EditRoom.vue";

export default {
  name: "MyRoom",
  components: {
    EditRoom,
  },
  data() {
    return {
      tableData: [],
      current: 1,
      size: 5,
      total: 100,
    };
  },
  mounted() {
    this.getTableData(1);
    this.$bus.$on("refreshRoomList", () => {
      this.getTableData(this.current);
    });
  },
  beforeDestroy() {
    this.$bus.$off("refreshRoomList");
  },
  methods: {
    priceFormat(price) {
      return price == "0"
        ? 0
        : (price =
            price.slice(0, price.length - 2) +
            "." +
            price.slice(price.length - 2));
    },
    tableIndex(index) {
      return (this.current - 1) * this.size + index + 1;
    },
    getTableData(current) {
      let loading = Loading.service({
        text: "正在加载...",
        target: ".myroom_box",
      });
      axios
        .get("/room/getRoomListByUploaderId", {
          params: {
            current: current,
            size: this.size,
            uploaderId: this.$store.state.userModule.user.user.id,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.current = current;
            this.total = res.data.data.total;
            this.tableData = res.data.data.records;
          } else {
            this.$message.error(res.data.data);
          }
          loading.close();
        })
        .catch((e) => {});
    },
    handleEdit(index, row) {
      let editRoom = JSON.parse(JSON.stringify(row));
      editRoom.price = this.priceFormat(editRoom.price);
      this.$bus.$emit("updateRoom", editRoom);
      this.$refs.editDrawer.openDrawer();
    },
    handleDelete(index, row) {
      let loading = Loading.service({
        text: "正在加载...",
        target: ".myroom_box",
      });
      axios
        .get("/room/deleteRoomById", {
          params: {
            id: row.id,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success(res.data.data);
            if (this.total % this.size == 1) {
              this.getTableData(this.current - 1);
            } else {
              this.getTableData(this.current);
            }
          } else {
            this.$message.error(res.data.data);
          }
          loading.close();
        });
    },
  },
};
</script>

<style scoped>
.option {
  display: flex;
  flex-wrap: nowrap;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>